var AI = {
    moveChess: function() {
        var maxX = 0,
            maxY = 0,
            maxWeight = 0,
            i, j, tem;
        for (i = Chess.gridCnt-1; i >= 0; i--) {
            for (j = Chess.gridCnt-1; j >= 0; j--) {
                if (Chess.chessArr[i][j] !== 0) {
                    continue;
                }
                tem = this.calcWeight(i, j);
                if (tem > maxWeight) {
                    maxWeight = tem;
                    maxX = i;
                    maxY = j;
                }
            }
        }
        setTimeout(Chess.markChess(maxX, maxY), 1000); 
        if ((maxWeight >= 100000 && maxWeight < 250000) || (maxWeight >= 500000)) {
            Game.showResult(-1);
            return;
        } else {
            Chess.isBlack = true;
        }
    },
    putDirectX: function(i, j, chessColor) {
        var m, n,
            nums = 1,
            side1 = false,
            side2 = false;
        for (m = j - 1; m >= 0; m--) {
            if (Chess.chessArr[i][m] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[i][m] === 0) {
                    side1 = true;
                }
                break;
            }
        }
        for (m = j + 1; m < Chess.gridCnt; m++) {
            if (Chess.chessArr[i][m] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[i][m] === 0) {
                    side2 = true;
                }
                break;
            }
        }
        return {
            "nums": nums,
            "side1": side1,
            "side2": side2
        };
    },
    putDirectY: function(i, j, chessColor) {
        var m, n,
            nums = 1,
            side1 = false,
            side2 = false;
        for (m = i - 1; m >= 0; m--) {
            if (Chess.chessArr[m][j] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][j] === 0) {
                    side1 = true;
                }
                break;
            }
        }
        for (m = i + 1; m < Chess.gridCnt; m++) {
            if (Chess.chessArr[m][j] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][j] === 0) {
                    side2 = true;
                }
                break;
            }
        }
        return {
            "nums": nums,
            "side1": side1,
            "side2": side2
        };
    },
    putDirectXY: function(i, j, chessColor) {
        var m, n,
            nums = 1,
            side1 = false,
            side2 = false;
        for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
            if (Chess.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][n] === 0) {
                    side1 = true;
                }
                break;
            }
        }
        for (m = i + 1, n = j + 1; m < Chess.gridCnt && n < Chess.gridCnt; m++, n++) {
            if (Chess.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][n] === 0) {
                    side2 = true;
                }
                break;
            }
        }
        return {
            "nums": nums,
            "side1": side1,
            "side2": side2
        };
    },
    putDirectYX: function(i, j, chessColor) {
        var m, n,
            nums = 1,
            side1 = false,
            side2 = false;
        for (m = i - 1, n = j + 1; m >= 0 && n < Chess.gridCnt; m--, n++) {
            if (Chess.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][n] === 0) {
                    side1 = true;
                }
                break;
            }
        }
        for (m = i + 1, n = j - 1; m < Chess.gridCnt && n >= 0; m++, n--) {
            if (Chess.chessArr[m][n] === chessColor) {
                nums++;
            } else {
                if (Chess.chessArr[m][n] === 0) {
                    side2 = true;
                }
                break;
            }
        }
        return {
            "nums": nums,
            "side1": side1,
            "side2": side2
        };
    },
    calcWeight: function(i, j) {
        var weight = 14 - (Math.abs(i - 7) + Math.abs(j - 7)), 
            pointInfo = {}, 
            chessColor = Chess.isBlack === true ? 1 : -1;
        pointInfo = this.putDirectX(i, j, chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true); 
        pointInfo = this.putDirectX(i, j, -chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false); 
        pointInfo = this.putDirectY(i, j, chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true); 
        pointInfo = this.putDirectY(i, j, -chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false); 
        pointInfo = this.putDirectXY(i, j, chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true); 
        pointInfo = this.putDirectXY(i, j, -chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false); 
        pointInfo = this.putDirectYX(i, j, chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true); 
        pointInfo = this.putDirectYX(i, j, -chessColor);
        weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false); 
        return weight;
    },
    weightStatus: function(nums, side1, side2, isAI) {
        var weight = 0;
        switch (nums) {
            case 1:
                if (side1 && side2) {
                    weight = isAI ? 15 : 10; 
                }
                break;
            case 2:
                if (side1 && side2) {
                    weight = isAI ? 100 : 50; 
                } else if (side1 || side2) {
                    weight = isAI ? 10 : 5; 
                }
                break;
            case 3:
                if (side1 && side2) {
                    weight = isAI ? 500 : 200; 
                } else if (side1 || side2) {
                    weight = isAI ? 30 : 20; 
                }
                break;
            case 4:
                if (side1 && side2) {
                    weight = isAI ? 5000 : 2000; 
                } else if (side1 || side2) {
                    weight = isAI ? 400 : 100; 
                }
                break;
            case 5:
                weight = isAI ? 100000 : 10000; 
                break;
            default:
                weight = isAI ? 500000 : 250000;
                break;
        }
        return weight;
    }
};
